using System;
using System.Globalization;
using Medianamik.Core;
using NUnit.Framework;
using System.Linq;

namespace Medianamik.Tests.Integration.Medianamik.UI.Web
{
    [TestFixture]
    public class NodeManagementTest
    {
        [Test]
        [Ignore("Ignore Test - See App.Config Connection String")]
        public void Saving_node_with_many_to_many_relations_does_not_throw_exceptions()
        {
            const string returnUrl = "allo";

            NodeManager.CacheProvider = new FakeNodeCacheProvider();
            var nodeManagement = new NodeManagementFake();
            nodeManagement.CurrentEditionCulture = CultureInfo.GetCultureInfo("fr-ca");
            var node = NodeManager.GetNode(new Guid("1f4f3347-ef27-473a-841c-0d0c41368d78"));
            //new Node(new Guid("1f4f3347-ef27-473a-841c-0d0c41368d78"), new Guid("1ac94bf8-ce58-4c37-bb92-0b74dccc66df"), new Guid("1b22d8d2-0899-4802-ab1a-ff1232501059"));

            var property = node[nodeManagement.CurrentEditionCulture].Properties["WebPageId"] as ManyToManyNodeProperty;
            var options = property.GetOptions(nodeManagement.CurrentEditionCulture);
            var values = property.Values;

            var notConserved = values.First();
            var newValues = options.Select(o => o.NodeId)
                .Except(new[] { notConserved });

            property.SetValue(newValues);

            //var manyToManyNodes = property
            //    .GetCurrentRelationsValues();

            //Assert.AreEqual(EntityStates.Loaded, property.GetRemovedRelationsValues()
            //    .Select(n=>((ManyToManyNodeProperty)n.GetRightNodeProperty(property.Relation )). )
            //    .First(n => n.ID == notConserved).Status);

            //manyToManyNodes.ToList()
            //    .ForEach(n => Assert.AreEqual(EntityStates.New, n.Status));

            //manyToManyNodes.Except(property.InitialValues).ToList()
            //    .ForEach(n => Assert.That(n.Status, Is.EqualTo(EntityStates.New)));


            var nodeWrapper = new NodeWrapper { InnerNode = node };
            nodeManagement.CurrentNode = nodeWrapper;
            nodeManagement.SetReturnUrl(returnUrl);

            nodeManagement.Validate();
            nodeManagement.btnSaveAndPublish_Click(null, null);

            Assert.AreEqual(returnUrl, nodeManagement.ExpectedUrl);
        }
    }
}